<template>
    <div class="content">
        <el-row>
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
            <el-table-column prop="name" label="姓名" show-overflow-tooltip align="center" />
            <el-table-column prop="mobile" label="手机号码" show-overflow-tooltip align="center" />
            <el-table-column prop="created_at" label="创建时间" width="220" align="center" />
            <el-table-column prop="status_text" label="状态" width="80" align="center" />
            <el-table-column fixed="right" label="操作" width="160" align="center">
                <template #default="scope">
                    <el-link type="primary" @click="onEdit(scope.row.id)" >编辑</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

   
</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import { CirclePlus, RefreshRight, Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../../util/request';
import router from '../../../../router';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',role_type:2,page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
interface User {
    id: number
    name: string
    mobile: string
    projects: number
    created_at: string
}
const tableData = ref<User[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/labourer',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    form.value.page = 1
    onGetData()
}


const onEdit = (id: number) => {
    router.push({ name: 'labourersEdit',params: { id: id }})
}

// const onStatus = (id: number) => {
//     http.patch('/labourer/status',{id:id}).then((response:any)=>{
//         if(response.data.code != 200){
//             ElMessage.error(response.data.msg);return;
//         }
//         ElMessage.success(response.data.msg);
//         onGetData();
//     })
// }

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>